//
//  Gift_SelView.m
//  MindBox
//
//  Created by MS on 16-1-8.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//
#define margin 10
#import "Gift_SelView.h"
#import "UIImageView+WebCache.h"
#import "SelBase_View.h"
#import "UIView+Frame.h"
@interface Gift_SelView ()
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (nonatomic,strong) NSMutableArray * selArray;
@end

@implementation Gift_SelView

-(void)setGift_SelData:(NSArray *)gift_SelData
{
    _gift_SelData = gift_SelData;
    
    [self.selArray removeAllObjects];
    
        int i = 0;
        for (Gift_SeleData * data in gift_SelData) {
            
            SelBase_View * baseView = [SelBase_View gift_SelBaseViewWithXib];
            [self addSubview:baseView];
            [self.selArray addObject:baseView];
            baseView.gift_Sel = data;
            
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidCick:)];
            [baseView addGestureRecognizer:tap];
            
            baseView.tag = i;
            i++;
        }
}

-(void)viewDidCick:(UIGestureRecognizer  *)tap
{
    if (self.SelViewIndexBlock) {
        _SelViewIndexBlock(tap.view.tag);
    }
}

-(NSMutableArray *)selArray
{
    if (_selArray==nil) {
        _selArray = [[NSMutableArray alloc] init];
    }
    return _selArray;
}

-(void)setGiftData:(GiftPageData *)giftData
{
    _giftData = giftData;
    
    self.titleView.text = giftData.topic_title;
}

+(instancetype) gift_SelViewWithXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}


-(void)layoutSubviews
{
    [super layoutSubviews];

    NSInteger count =self.selArray.count;
    CGFloat y0 = CGRectGetMaxY(_titleView.frame);
    CGFloat h = (self.height - y0-(count+1)*margin)/count;
    CGFloat w = (self.width -2*margin);
    int i = 0;
    for (SelBase_View * view in self.selArray) {
        
         CGFloat y =  y0 +margin + (margin+h)*i;
        
        view.frame = CGRectMake(margin,y,w,h);
        i++;
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
